package tienda.zapateria.siszap.da;

import java.sql.Connection;
import java.util.ArrayList;
import org.rx.cr.dao.DAOAbstract;
import tienda.zapateria.siszap.be.BEProveedor;

public class DAProveedor extends DAOAbstract<BEProveedor> {

    public DAProveedor(Connection c) {
        setConnection(c);
    }

    @Override
    public int registrar(BEProveedor be) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_ins_proveedor");
            setParameterString(be.getRazonsocial());
            setParameterString(be.getRuc());
            setParameterString(be.getTelefono());
            setParameterString(be.getCorreo());
            setParameterString(be.getDireccion());
            setParameterInt(be.getUbigeoid());

            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int actualizarRegistro(BEProveedor be) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_upd_proveedor");
            setParameterInt(be.getProveedorid());
            setParameterString(be.getRazonsocial());
            setParameterString(be.getRuc());
            setParameterString(be.getTelefono());
            setParameterString(be.getCorreo());
            setParameterString(be.getDireccion());
            setParameterInt(be.getUbigeoid());
            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int eliminarRegistro(BEProveedor be) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_del_proveedor");

            setParameterInt(be.getProveedorid());

            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public ArrayList<BEProveedor> buscarReferencia(String string) throws Exception {
        try {
            clear();
            setStoreProcedure("zapateria.sp_sel_proveedor");
            setParameterString(string);
            commitc();
            while (existResult()) {
                BEProveedor be = new BEProveedor();
                be.setProveedorid(getDataInt(1));
                be.setRazonsocial(getDataString(2));
                be.setRuc(getDataString(3));
                be.setTelefono(getDataString(4));
                be.setCorreo(getDataString(5));
                be.setDireccion(getDataString(6));
                be.setUbigeoid(getDataInt(7));

                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public ArrayList<BEProveedor> listar() throws Exception {
        try {
            clear();
            setStoreProcedure("zapateria.sp_sel_proveedor");
            setParameterString("");
            commitc();
            while (existResult()) {
                BEProveedor be = new BEProveedor();
                be.setProveedorid(getDataInt(1));
                be.setRazonsocial(getDataString(2));
                be.setRuc(getDataString(3));
                be.setTelefono(getDataString(4));
                be.setCorreo(getDataString(5));
                be.setDireccion(getDataString(6));
                be.setUbigeoid(getDataInt(7));

                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public BEProveedor buscar(int i) throws Exception {
        try {
            BEProveedor be = null;
            clear();
            setStoreProcedure("zapateria.sp_sel_proveedor_id");
            setParameterInt(i);
            commitc();
            while (existResult()) {
                be = new BEProveedor();
                be.setProveedorid(getDataInt(1));
                be.setRazonsocial(getDataString(2));
                be.setRuc(getDataString(3));
                be.setTelefono(getDataString(4));
                be.setCorreo(getDataString(5));
                be.setDireccion(getDataString(6));
                be.setUbigeoid(getDataInt(7));

            }
            return be;
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

}
